﻿using System;
using NQueries.Utils;

namespace NQueries
{
    /// <summary>
    /// Contains various extension methos for <see cref="IDataContext"/>.
    /// </summary>
    public static class DataContextExtensions
    {
        /// <summary>
        /// Gets an entity by its type and id.
        /// </summary>
        /// <param name="dataContext"></param>
        /// <param name="type"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static object GetById(this IDataContext dataContext, Type type, object id)
        {
            Check.IsNotNull(dataContext, "dataContext");
            Check.IsNotNull(type, "type");

            object entity = dataContext.Find(type, id);
            if (entity == null)
                throw new NQueriesException("object of type <{0}> with id <{1}> not found".FormatWith(type, id));
            return entity;
        }
    }
}